<?php

namespace common\observer;

use common\service\StatusService;
use support\SingletonTrait;
use Webman\Stomp\Client;
use teamones\dbeventObserver\interfaces\SplObserver;
use teamones\dbeventObserver\interfaces\SplSubject;

/**
 * 内部任务完成 检查下游任务的组装状态
 */
class TaskDoneDownstreamAssembleCheck implements SplObserver
{
    use SingletonTrait;

    /**
     * @inheritDoc
     */
    function update(SplSubject $subject): void
    {
        $updateData = $subject->getSplUpdateData();
        if (!isset($updateData['task_status'])) {
            return;
        }
        $task = $subject->getSubjectData();
        if ($task['step_type'] != 'step') {
            return;
        }
        if ($updateData['task_status'] != StatusService::getInstance()->getTaskDoneStatus()['id']) {
            return;
        }
        if ($task['executor_tenant_id'] != 0 && $task['tenant_id'] != $task['executor_tenant_id']) {
            return;
        }
        try {
            Client::send('task_downstream_assemble_check', build_queue_data_with_xu(['task_id' => $subject->getSplPrimaryId()]));
        } catch (\Throwable $e) {
            trace(format_exception_info($e), 'ERR');
        }
    }

}
